/*--------------首页------------------*/
var main=document.getElementById('main');
var aDiv=document.querySelectorAll('.list');

var len=aDiv.length;
var page=1;
var onOff=true;


getList();
async function getList(){
	var str=await Ajax('GET','http://39.105.192.164:9090/apitest/goodslist','pager='+page);
	var data=JSON.parse(str).data.list;

	data.forEach(function(item){
		var _index=getShort();
		var oDiv=document.createElement('div');
		oDiv.innerHTML=`
			<a target="_blank" href=http://localhost/mogujie/html/goodsDetail.html?${item.id}>
				<img src=${item.pic}>
			</a>
		`

		aDiv[_index].appendChild(oDiv);
	})
	onOff=true;
}
	var aA=document.querySelectorAll('.list a');


//鼠标滚动加载
window.onscroll=function(){
	var _index=getShort();
	var oDiv=aDiv[_index];
	var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;

	if(getTop(oDiv)+oDiv.offsetHeight>document.documentElement.clientHeight+scrollTop){
		if(onOff){
			onOff=false;
			page++;
			getList();
		}
	}

}


//得到最短li的索引
function getShort(){
	var index=0;
	var ih=aDiv[index].offsetHeight+aDiv[index].offsetTop;
	for(var i=1;i<len;i++){
		if(aDiv[i].offsetHeight+aDiv[i].offsetTop<ih){
			index=i;
			ih=aDiv[i].offsetHeight;
		}
	}
	return index;
}

//得到div的offsetTop

function getTop(obj){
	var iTop=0;
	while(obj){
		iTop+=obj.offsetTop;
		obj=obj.offsetParent;
	}
	return iTop;
}

/*关于我们显示隐藏 */

var oA=document.querySelector('#top_content a:last-child');
var aboutUs=document.getElementById('about_us');
var timer=null;
oA.onmouseover=function(){
	show(aboutUs);
}
oA.onmouseout=function(){
	hide(aboutUs);
}
aboutUs.onmouseover=function(){
	show(aboutUs);
}
aboutUs.onmouseout=function(){
	hide(aboutUs);
}

function show(obj){
	clearInterval(timer)
	obj.style.display='block';
}
function hide(obj){
	timer=setTimeout(function(){
		obj.style.display='none';
	},200)
}
/*目录显示隐藏*/
var cateBtn=document.getElementById('cate_btn');
var category=document.getElementById('category');

cateBtn.onmouseover=function(){
	show(category);
}
cateBtn.onmouseout=function(){
	hide(category);
}
category.onmouseover=function(){
	show(this);
}
category.onmouseout=function(){
	hide(this);
}
/*下载显示隐藏*/
var download=document.getElementById('download');
var appCode=document.getElementById('app_code');

download.onmouseover=function(){
	show(appCode);
}
download.onmouseout=function(){
	hide(appCode);
}
appCode.onmouseover=function(){
	show(this);
}
appCode.onmouseout=function(){
	hide(this);
}

/*---------------轮播----------------*/

var aImg=document.getElementById('wheel_plant').getElementsByTagName('img');
var num=0;
//初始化
aImg[num].style.left=num;

autoPlay();
function autoPlay(){
	clearTimeout(timer);
	timer=setTimeout(function(){
			var that=aImg[num];
			doMove(that,'left',20,-710,function(){//图片运动到-400，即运动到显示区左边
				that.style.left='710px';//随后恢复到显示区右边
			})

			if(num==aImg.length-1){
				num=-1;
			}
			num++;
			doMove(aImg[num],'left',20,0,function(){
					autoPlay();
			})


	},2000)
}


/*-----------运动封装-------------*/
function doMove(obj,attr,dir,target,endFn){

	dir=parseInt(getStyle(obj,attr))<target?dir:-dir;//小于目标点方向为正，否则为负
	clearInterval(obj.timer);
	obj.timer=setInterval(function(){

		var speed=parseInt(getStyle(obj,attr))+dir;//步长
		/*if(speed>target&&dir>0){//往前跑
			speed=target;
		}
		if(speed<target&&dir<0){//往后跑
			speed=target;
		}*/
		if(speed>target&&dir>0||speed<target&&dir<0){
			speed=target;//(43,45)等同于(37,42)
		}
		obj.style[attr]=speed+'px';

		if(speed==target){
			clearInterval(obj.timer);

			/*if(endFn){
				endFn();
			}*/
			endFn&&endFn();//等同于53-55
		}
	},30);
}
function getStyle(obj,attr){
	return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr];
}

/*--------------判断购物车中是否有内容，有则加红点------------------*/

var redDot=document.querySelector('#red_dot span');

// console.log(JSON.parse(localStorage.getItem('result')).length)
if(JSON.parse(localStorage.getItem('result')).length){
	console.log(localStorage.getItem('result'))
	redDot.className='redDot'
}